<?php
namespace app\crm\model;

use think\Model;
use think\Db;
use think\Cache;


class Goods extends Model
{   
    /**
     * 缓存商品信息
     */
    protected function goodsCache ()
    {
		$join = [
			['xy_shop_category c','g.cate_id = c.id'],
			['xy_shop_brand b','g.brand_id = b.id'],
			['xy_shop_type t','g.good_type =  t.id']
		];
		$result = Db::table('xy_shop_goods')->order("g.good_order desc")->field("g.id,g.good_name,g.good_num,g.good_Inventory,g.price,g.cate_id,g.brand_id,g.simple_description,g.good_description,g.good_order,g.good_enabled,g.good_type,g.good_attribute,c.cat_name,b.brand_name,t.caty_name")->alias('g')->join($join)->select();
        // 以工号为键
        foreach ($result as $val) {
            $goodsInfo[$val['id']] = $val;
        }
		
        Cache::set('goodsInfo', $goodsInfo);
    }
	/**
     * 添加商品
     * @param  [arr] $data     [添加商品的数据]
     * @param  [str] $good_name [商品名称]
     */
    public function insertGoods($data,$good_num)
	{
		$num_count = Db::name('shop_goods')->where("good_name ='$good_name'")->count();
		if($num_count>0){
			$user_mess = "该商品货号已存在！";
			echo json_encode(array('msg'=>$user_mess));
			exit;
		}
        $result = Db::name('shop_goods')->insert($data);
		$this->goodsCache();
        if ($result){
            echo json_encode(array('success'=>true,'msg'=>'添加成功！'));
        } else {
            echo json_encode(array('msg'=>'添加失败！'));
        }
    }

    /**
     * 获取商品列表
     * @param  int $page <页码>
     * @param  int $limit <每页显示条数>
     * @return array <商品类型列表信息>
     */
    public function getGoodsData($page,$limit,$where)
    {
		
		$join = [
			['xy_shop_category c','g.cate_id = c.id'],
			['xy_shop_brand b','g.brand_id = b.id'],
			['xy_shop_type t','g.good_type =  t.id']
		];
        $count = Db::table('xy_shop_goods')->alias('g')->field("g.id,g.good_name,g.good_num,g.good_Inventory,g.price,g.cate_id,g.brand_id,g.simple_description,g.good_description,g.good_order,g.good_enabled,g.good_type,g.good_attribute,c.cat_name,b.brand_name,t.caty_name")->where($where)->join($join)->count();
		
        // 查询状态为1的用户数据 并且每页显示10条数据
		
        $dataList = Db::table('xy_shop_goods')->order("g.createtime desc")->field("g.id,g.good_name,g.good_num,g.good_Inventory,g.price,g.cate_id,g.brand_id,g.simple_description,g.good_description,g.good_order,g.good_enabled,g.good_type,g.good_attribute,c.cat_name,b.brand_name,t.caty_name")->where($where)->limit(($page-1)*$limit,$limit)->alias('g')->join($join)->select();
		//echo Db::name('shop_goods')->getLastSql();die;
        foreach($dataList as &$val){
			$val["good_attribute"] = json_decode($val["good_attribute"],true);
			if(!empty($val["good_attribute"])){
				foreach($val["good_attribute"] as &$vm){
					$vm["name"] = $vm["attribute_name"].":".$vm["attribute_value"];
					$val["attribute"] .= $vm["attribute_label"].": ".$vm["attribute_value"].",  ";
				}
			}
			$val["attribute"] = rtrim($val["attribute"],",  ");
            $val["goods_num"] = 1;
		}
        // 
        $rowsList = count($dataList) ? $dataList : false;
        $arrCate["code"] = 0;
        $arrCate["msg"] = '';
        $arrCate["count"] = $count;
        $arrCate["data"] = $rowsList;
        return $arrCate;
    }

    /**
     * 获取单个商品的信息
     * @param  [int] $id [商品id]
     * @return [arr]     [商品的信息]
     */
    public function getGoodsArr($id)
    {
        $arrData = Db::name('shop_goods')->where('id',$id)->find();
        return $arrData;
    }

    /**
     * 更新商品信息
     * @param  [int] $id   [商品id]
     * @param  [arr] $data [商品数据]
     */
    public function updateGoods($id,$data)
    {
        $result = Db::name('shop_goods')->where('id', $id)->update($data);
        //echo Db::name('shop_goods')->getLastSql();die;
		if ($result){
			$this->goodsCache();
            echo json_encode(array('success'=>true,'msg'=>'更新成功！'));
        } else {
            echo json_encode(array('msg'=>'更新失败！'));
        }
    }


    /**
     * 删除商品
     * @param  int      $id     [商品id]
     */
    public function delGoods($id)
    {
        $result = Db::name('shop_goods')->where('id',$id)->delete();
		$this->goodsCache();
        if ($result){
            echo json_encode(array('success'=>true,'msg'=>'删除成功！'));
        } else {
            echo json_encode(array('msg'=>'删除失败！'));
        }
    }
	
	//类型
	public function getShopType()
	{
		$goodTypeData = Db::name("shop_type")->order("cat_order asc")->field("id,caty_name")->where("caty_enabled = 'Y'")->select();
		return $goodTypeData;
	}
	//分类
	public function getShopCategory()
	{
		$goodCategoryData = Db::name("shop_category")->order("sort_orderid asc")->field("id,cat_name")->where("cat_enabled = 'Y'")->select();
		return $goodCategoryData;
	}
	//品牌
	public function getShopBrand()
	{
		$goodBrandData = Db::name("shop_brand")->order("brand_order asc")->field("id,brand_name")->where("brand_enabled = 'Y'")->select();
		return $goodBrandData;
	}
	//属性
	public function attributeValues($id){
		$arrData = Db::name("shop_attribute")->where("type_id = '$id'")->select();
		return $arrData;
	}
	
    /**
     * 获取商品属性
     * @param [int] $id [商品类型id]
     */
	function addAttribute($id){
		$arrData = Db::name("shop_attribute")->where("type_id = '$id'")->select();
		foreach($arrData as $val){
			$fieldValue[] = array("attribute_label"=>$val["attr_name"],"attribute_name"=>$val["attr_en_name"],"attribute_value"=>$_POST[$val["attr_en_name"]],"text_type"=>$val["text_type"],"select_value"=>$val["attr_values"]);
		}
		$strAttribute = json_encode($fieldValue);
		return $strAttribute;
	}


}